<?php
namespace app\api\controller;
use think\Input;
use think\Controller;
use app\common\controller\Base;
class Email extends Base{
	public function _initialize()
    {
        parent::_initialize();
    }
	public function send(){
		ignore_user_abort(true);
		set_time_limit(0);
		if(file_exists("email.lock")){
			return false; 
		}
		file_put_contents('email.lock', '');
		$results = db('emails')->select();
		foreach($results as $vo){
			$vo['content'] = str_replace('data-color="#EEEEEE"', 'bgcolor="#EEEEEE"', $vo['content']);
			$vo['content'] = str_replace('data-color="#FFFFFF"', 'bgcolor="#FFFFFF"', $vo['content']);
			$vo['content'] = str_replace('data-style="style"', 'style="border:1px solid #ccc; padding:5px 10px;font-size:13px;"', $vo['content']);
			$vo['content'] .= '<p>'.lang('email-footer').'</p>';
			$emails = explode(",", $vo['email']);
			if(is_array($emails)){
				foreach($emails as $email){
					if(is_email($email)){
						$result = send_email($email,$vo['title'],$vo['content']);
					}
					sleep(3);
				}
			}
		    db('emails')->where('id',$vo['id'])->delete();
		}
		unlink("email.lock");
		file_put_contents("email.log", date("Y-m-d H:i")."\n",FILE_APPEND);
		exit('Success');
	}
	
}